package is.glif.fenius.viz.http;

import is.glif.fenius.ws.draft.gen.Draft;
import is.glif.fenius.ws.draft.gen.DraftFeniusService;
import is.glif.fenius.ws.draft.gen.ExternalFault_Exception;
import is.glif.fenius.ws.draft.gen.InternalFault_Exception;
import is.glif.fenius.ws.draft.gen.ListReq;
import is.glif.fenius.ws.draft.gen.ListResponse;
import is.glif.fenius.ws.draft.gen.ReservationParams;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

public class FeniusList extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static Logger LOG = Logger.getLogger(FeniusList.class);
    private static final QName SERVICE_NAME = new QName("http://fenius.glif.is/ws/draft/", "draft");
    private DraftFeniusService port = null;

    public void init() {
        LOG.info("init.start");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LOG.info("FeniusList.start");
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter pw = response.getWriter();
        JSONArray jResponse = new JSONArray();
        
        
        jResponse = JSONArray.fromObject(this.doList(pw));
        
        
        String output = "";
        try {
            output = jResponse.toString(2);
        } catch (JSONException e) {
            e.printStackTrace(pw);
        }
        pw.println(output);
        LOG.info("FeniusList.end");

    }
    
    private List<ReservationParams> doList(PrintWriter pw) {
        
        URL wsdlURL = null;
        try {
            wsdlURL = new URL("http://jupiter.es.net:8080/DraftFeniusService?wsdl");
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            System.exit(1);
        }
        
        Draft ss = new Draft(wsdlURL, SERVICE_NAME);
        port = ss.getDraftFeniusServicePort();
        
        
        is.glif.fenius.ws.draft.gen.List wrapReq = new is.glif.fenius.ws.draft.gen.List();
        ListReq listReq = new ListReq();
        wrapReq.setArg0(listReq);
        try {
            ListResponse resp = port.list(wrapReq);
            List<ReservationParams> resvs = resp.getReturn().getReservations();
            return resvs;
        } catch (InternalFault_Exception e) { 
            System.out.println("InternalFault has occurred.");
            e.printStackTrace();
            System.out.println(e.toString());
        } catch (ExternalFault_Exception e) { 
            System.out.println("ExternalFault has occurred.");
            e.printStackTrace();
            System.out.println(e.toString());
        }
        return null;
    }

}
